﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using Hartley.Util;

namespace WebApplication1
{
    public partial class viewAllExCoRolesOfCommMember : System.Web.UI.Page
    {
        string queryString;
        MemberAL memberAL;
        SportsAL sportsAL;
        Guid comMemberId;
        protected int currentPageNumber = 1;
        private const int PAGE_SIZE = 5;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                queryString = Request.QueryString.ToString();
                comMemberId = Guid.Empty;
                bool convertableToBool = Guid.TryParse(queryString, out comMemberId);
                if (convertableToBool)
                {
                    BindData();
                }
                else
                {
                    return;
                }

            }
        }


        private void BindData()
        {

            memberAL = new MemberAL();
            sportsAL = new SportsAL();

            List<CommitteeMemberDTO> allRoleOfComMember = memberAL.getAllRolseOfCommMember(comMemberId).ToList();
            List<CommitteeMemberDTO> allRoleOfComMemberOfOnePage = new List<CommitteeMemberDTO>();
            MemberDTO membrDTO = memberAL.GetMemberById(comMemberId).FirstOrDefault();
            HyperLink hlMemberName = new HyperLink();
            hlMemberName.NavigateUrl = "viewMember.aspx?" + membrDTO.MemberId;
            hlMemberName.Text = membrDTO.FullName;
            memberAllRoles.InnerHtml = "The past role(s) played by <a style=\"color:White;>\" href=viewMember.aspx?" +membrDTO.MemberId+">"+ membrDTO.FullName + "</a>  at Col PPA";
            //gvViewAllMembers.DataSource = allMembers;
            //gvViewAllMembers.DataBind();


            // get the total rows 
            double totalRows = (int)allRoleOfComMember.Count;

            lblAllRolesOfComMembrTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            lblAllRolesOfComMembrCurrPage.Text = currentPageNumber.ToString();

            lblAllRolesOfComMembrPage.Text = "Displaying all roles of a committee member - Page ";
            lblAllRolesOfComMembrOf.Text = "  of  ";

            int totalPageNumber = lblAllRolesOfComMembrTotalPage.Text == "" ? 0 : Convert.ToInt32(lblAllRolesOfComMembrTotalPage.Text);

            if (totalPageNumber == 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllRolesOfComMembrFirst.Enabled = false;
                    btnAllRolesOfComMembrPrev.Enabled = false;
                    btnAllRolesOfComMembrNext.Enabled = false;
                    btnAllRolesOfComMembrLast.Enabled = false;
                    allRoleOfComMemberOfOnePage = allRoleOfComMember;
                }

            }
            else if (totalPageNumber > 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllRolesOfComMembrFirst.Enabled = false;
                    btnAllRolesOfComMembrPrev.Enabled = false;
                    btnAllRolesOfComMembrNext.Enabled = true;
                    btnAllRolesOfComMembrLast.Enabled = true;
                    allRoleOfComMemberOfOnePage = allRoleOfComMember.GetRange(0, PAGE_SIZE);
                }
                else if (currentPageNumber < totalPageNumber && currentPageNumber > 1)
                {
                    btnAllRolesOfComMembrFirst.Enabled = true;
                    btnAllRolesOfComMembrPrev.Enabled = true;
                    btnAllRolesOfComMembrNext.Enabled = true;
                    btnAllRolesOfComMembrLast.Enabled = true;
                    allRoleOfComMemberOfOnePage = allRoleOfComMember.GetRange(((currentPageNumber - 1) * PAGE_SIZE), PAGE_SIZE);

                }
                else if (currentPageNumber == totalPageNumber)
                {
                    btnAllRolesOfComMembrFirst.Enabled = true;
                    btnAllRolesOfComMembrPrev.Enabled = true;
                    btnAllRolesOfComMembrNext.Enabled = false;
                    btnAllRolesOfComMembrLast.Enabled = false;
                    allRoleOfComMemberOfOnePage = allRoleOfComMember.GetRange(((currentPageNumber - 1) * PAGE_SIZE), (allRoleOfComMember.Count - (currentPageNumber - 1) * PAGE_SIZE));
                }
            }
            

            CreateDynamicTable(allRoleOfComMemberOfOnePage);

        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = (int)Math.Ceiling(totalRows / PAGE_SIZE);

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllRolesOfComMembrCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllRolesOfComMembrCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllRolesOfComMembrTotalPage.Text);
                    break;
            }


            BindData();
        }


        private void CreateDynamicTable(List<CommitteeMemberDTO> allRoleOfComMemberOfOnePage)
        {
            PlaceHolder4.Controls.Clear();

            // Fetch the number of Rows and Columns for the table 
            // using the properties


            int tblRows = allRoleOfComMemberOfOnePage.Count;

            List<CommitteeDTO> committeeDTO = new List<CommitteeDTO>();
            List<MemberDTO> memberDTO = new List<MemberDTO>();
            

            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder4.Controls.Add(tbl);

            TableRow trHeader = new TableRow();
            trHeader.Width = new Unit("25%");

            HyperLink hlRolesOfComMembrHeaderCommName = new HyperLink();
            hlRolesOfComMembrHeaderCommName.Text = "Committee Name";

            HyperLink hlRolesOfComMembrCellHeaderMembrFrm = new HyperLink();
            hlRolesOfComMembrCellHeaderMembrFrm.Text = "Member From";

            HyperLink hlRolesOfComMembrCellHeaderMemberTo = new HyperLink();
            hlRolesOfComMembrCellHeaderMemberTo.Text = "Member To";


            HyperLink hlRolesOfComMembrCellHeaderRole = new HyperLink();
            hlRolesOfComMembrCellHeaderRole.Text = "Role";

            List<HyperLink> allHeaderHyperlinks = new List<HyperLink>();

            //based  on selected type change column orders

            allHeaderHyperlinks.Add(hlRolesOfComMembrHeaderCommName);
            allHeaderHyperlinks.Add(hlRolesOfComMembrCellHeaderMembrFrm);
            allHeaderHyperlinks.Add(hlRolesOfComMembrCellHeaderMemberTo);
            allHeaderHyperlinks.Add(hlRolesOfComMembrCellHeaderRole);



            foreach (HyperLink hyperlinkHeader in allHeaderHyperlinks)
            {
                TableCell tcHeader = new TableCell();
                tcHeader.BackColor = System.Drawing.Color.FromArgb(28,28,255);
                tcHeader.ForeColor = System.Drawing.Color.White;

                tcHeader.Controls.Add(hyperlinkHeader);
                trHeader.Cells.Add(tcHeader);



            }

            tbl.Rows.Add(trHeader);


            // Now iterate through the table and add your controls 
            for (int i = 0; i < tblRows; i++)
            {
                TableRow tr = new TableRow();
                tr.Width = new Unit("25%");

                committeeDTO = memberAL.GetCommitteeById(allRoleOfComMemberOfOnePage[i].CommitteId).ToList();
                memberDTO = memberAL.GetMemberById(allRoleOfComMemberOfOnePage[i].MemberId).ToList();
                

                HyperLink hlRolesOfComMembrCellComName = new HyperLink();

                //get the last part frm committee title colmns whch s 2004/2003
                hlRolesOfComMembrCellComName.Text = "Executive Committee "+ committeeDTO[0].Title.Substring(committeeDTO[0].Title.Length -9);

                HyperLink hlRolesOfComMembrCellMemFrm = new HyperLink();
                hlRolesOfComMembrCellMemFrm.Text = allRoleOfComMemberOfOnePage[i].MemberFrom.Value.ToShortDateString();

                HyperLink hlRolesOfComMembrCellMemTo = new HyperLink();
                hlRolesOfComMembrCellMemTo.Text = allRoleOfComMemberOfOnePage[i].MemberTo.Value.ToShortDateString();

                HyperLink hlRolesOfComMembrCellRole = new HyperLink();
                hlRolesOfComMembrCellRole.Text = allRoleOfComMemberOfOnePage[i].Position;

                queryString = Request.QueryString.ToString();



                hlRolesOfComMembrCellComName.NavigateUrl = "viewExecutiveCommitteeById.aspx?" + committeeDTO[0].CommitteeId;


                List<HyperLink> allHyperlinks = new List<HyperLink>();
                allHyperlinks.Add(hlRolesOfComMembrCellComName);
                allHyperlinks.Add(hlRolesOfComMembrCellMemFrm);
                allHyperlinks.Add(hlRolesOfComMembrCellMemTo);
                allHyperlinks.Add(hlRolesOfComMembrCellRole);




                foreach (HyperLink hyperlink in allHyperlinks)
                {
                    TableCell tc = new TableCell();



                    if (i % 2 == 1)
                    {
                        tc.BackColor = System.Drawing.Color.White;   //Style.Add("background-color", "White");
                        tc.ForeColor = System.Drawing.Color.FromArgb(28,28,255);;
                    }
                    else
                    {
                        tc.Style.Add("background-color", "#2828FF");
                        tc.ForeColor = System.Drawing.Color.White;

                    }



                    // Add the control to the TableCell
                    tc.Controls.Add(hyperlink);


                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }

                // Add the TableRow to the Table

                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }
    }
}